<template>
  <div>
    <el-tree
      :props="defaultProps"
      :load="loadChildren"
      lazy
      show-checkbox
      ref="tree"
    >
    <!-- <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{node.templateName || node.indicatorsName}}</span>
    </span> -->
    </el-tree>
    <el-button @click="handleClick">确定</el-button>
  </div>
</template>
<script>
import request from '../utils/request'
export default {
  data() {
    return {
      defaultProps: {
        children: "children",
        label: "templateName",
        isLeaf: 'leaf'
      },
      data: [],
    };
  },
  methods: {
    loadChildren(node, resolve) {
        console.log(node,'000000000');
        if (node.level === 0) {
            request.post('/api/crisdev/zbpt/tempIndicators/listReport?sid=R1104V2').then(res => {
                console.log(res);
                resolve(res.result)
            })
          return
        }
        if (node.level > 1) return resolve([]);
      if(node.data) {
        request.get(`/api/crisdev/zbpt/tempIndicators/listIndicators?sid=R1104V2&templateId=${node.data.templateId}`).then(res => {
            console.log(res,'9999999');
            const finaldata = res.result.map(item => {
                return {
                    pid: node.data.templateId,
                    templateId: item.indicators,
                    templateName: item.indicatorsName,
                    leaf: true
                }
            })
            console.log(finaldata,'pppppp');
            resolve(finaldata)
        })
      }
    },
    handleClick() {
        let tmpObj = {}, finalArr = []
        const selectNodes = this.$refs.tree.getCheckedNodes(true)
        console.log(selectNodes,'iiiiiiiii');
        selectNodes.forEach(item => {
            if(item.pid in tmpObj) {
                tmpObj[item.pid].push(item.templateId)
            }else tmpObj[item.pid] = [item.templateId]
        })
        console.log('tmpObj:', tmpObj);
        request.post('/api/crisdev/zbpt/tempIndicators/indicatorsDataInfo',{sid: 'R1104V2',chartType:'1',crossType:'1',tempIndiData:tmpObj})
    }
  },
};
</script>
